package com.zzq.controller;


import com.aliyuncs.exceptions.ClientException;
import com.zzq.domain.search.StaticResourcesSearchParamet;
import com.zzq.result.SmileResult;
import com.zzq.service.StaticeResourcesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RequestMapping("/admin/static")
@RestController
@Api(value = "静态资源管理", tags = {"静态资源管理"})
public class StaticResourcesController {

    @Autowired
    StaticeResourcesService staticeResourcesService;

    /**
     * 静态资源上传
     *
     * @param file
     * @param name
     * @param dir
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("上传")
    public SmileResult resourcesUpload(@RequestParam("file") MultipartFile file, @RequestParam("name") String name, @RequestParam("dir") String dir) {
        try {
            return staticeResourcesService.addResources(file, name, dir);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取静态资源连接
     *
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("获取静态资源列表")
    public SmileResult allResources(@RequestBody StaticResourcesSearchParamet searchParamet) {
        try {
            return staticeResourcesService.getALlResources(searchParamet);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/del")
    @ApiOperation("获取静态资源列表")
    public SmileResult delResources(@RequestParam("staticResourcesId") Long staticResourcesId) {
        return staticeResourcesService.delResources(staticResourcesId);
    }
}
